#!/usr/bin/env python
# -*- coding: utf-8 -*-


import wx


class FileListPanel(wx.Panel):

    def __init__(self, parent, *args, ** kwargs):
        wx.Panel.__init__(self, parent, *args, ** kwargs)

        # 放置list的sizer和按钮的sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        # 所有CheckListBox的sizer
        list_sizer = wx.BoxSizer(wx.HORIZONTAL)

        # region 新增文件部分的操作
        self.add_file_list = add_list = \
            wx.CheckListBox(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, [], 0)
        self.add_check = add_check = wx.CheckBox(self, wx.ID_ANY, label='All', style=wx.CHK_3STATE)
        self.generate_layout(list_sizer, add_list, add_check)
        # endregion

        # region 删除文件部分的操作
        self.del_file_list = del_list = \
            wx.CheckListBox(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, [], 0)
        self.del_check = del_check = wx.CheckBox(self, wx.ID_ANY, label='All', style=wx.CHK_3STATE)
        self.generate_layout(list_sizer, del_list, del_check)
        # endregion

        # region 变更文件部分的操作
        self.chg_file_list = chg_list = \
            wx.CheckListBox(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, [], 0)
        self.chg_check = chg_check = wx.CheckBox(self, wx.ID_ANY, label='All', style=wx.CHK_3STATE)
        self.generate_layout(list_sizer, chg_list, chg_check)
        # endregion

        # region 冲突文件部分的操作
        self.cfl_file_list = cfl_list = \
            wx.CheckListBox(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, [], 0)
        self.cfl_check = cfl_check = wx.CheckBox(self, wx.ID_ANY, label='All', style=wx.CHK_3STATE)
        self.generate_layout(list_sizer, cfl_list, cfl_check)
        # endregion

        # region 管理外的文件部分的操作
        self.unv_file_list = unv_list = \
            wx.CheckListBox(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, [], 0)
        self.unv_check = unv_check = wx.CheckBox(self, wx.ID_ANY, label='All', style=wx.CHK_3STATE)
        self.generate_layout(list_sizer, unv_list, unv_check)
        # endregion

        # region 按钮操作
        # check in 按钮
        self.ci_btn = ci_btn = wx.Button(self, id=wx.ID_ANY, label=u'↑', pos=wx.DefaultPosition,
                                         size=wx.Size(24, 24), style=wx.BU_EXACTFIT)
        # 属性设定按钮
        self.property_btn = ppt_btn = wx.Button(self, id=wx.ID_ANY, label=u'P', pos=wx.DefaultPosition,
                                                size=wx.Size(24, 24), style=wx.BU_EXACTFIT)
        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        button_sizer.Add(ci_btn, 0, wx.SHAPED)
        button_sizer.Add(ppt_btn, 0, wx.SHAPED)
        # endregion
        sizer.Add(list_sizer, 1, wx.EXPAND)
        sizer.Add(button_sizer, 1, wx.EXPAND)
        self.SetSizer(sizer)

        # region connection
        self.Bind(wx.EVT_CHECKBOX, self.on_all_add_check_change, add_check)
        self.Bind(wx.EVT_CHECKBOX, self.on_all_del_check_change, del_check)
        self.Bind(wx.EVT_CHECKBOX, self.on_all_chg_check_change, chg_check)
        self.Bind(wx.EVT_CHECKBOX, self.on_all_cfl_check_change, cfl_check)
        self.Bind(wx.EVT_CHECKBOX, self.on_all_unv_check_change, unv_check)
        self.Bind(wx.EVT_CHECKLISTBOX, self.on_add_check_change, add_list)
        self.Bind(wx.EVT_CHECKLISTBOX, self.on_del_check_change, del_list)
        self.Bind(wx.EVT_CHECKLISTBOX, self.on_chg_check_change, chg_list)
        self.Bind(wx.EVT_CHECKLISTBOX, self.on_cfl_check_change, cfl_list)
        self.Bind(wx.EVT_CHECKLISTBOX, self.on_unv_check_change, unv_list)
        # endregion

    def on_add_check_change(self, event):
        add_check = self.add_check
        add_list = self.add_file_list
        self.on_list_check_change(add_check, add_list)

    def on_del_check_change(self, event):
        del_check = self.del_check
        del_list = self.del_file_list
        self.on_list_check_change(del_check, del_list)

    def on_chg_check_change(self, event):
        chg_check = self.chg_check
        chg_list = self.chg_file_list
        self.on_list_check_change(chg_check, chg_list)

    def on_cfl_check_change(self, event):
        cfl_check = self.cfl_check
        cfl_list = self.cfl_file_list
        self.on_list_check_change(cfl_check, cfl_list)

    def on_unv_check_change(self, event):
        unv_check = self.unv_check
        unv_list = self.unv_file_list
        self.on_list_check_change(unv_check, unv_list)

    def on_list_check_change(self, check, lst):
        count = lst.GetCount()
        check_count = len(lst.GetCheckedStrings())
        check.Set3StateValue(wx.CHK_CHECKED if count == check_count
                             else wx.CHK_UNCHECKED if check_count == 0 else wx.CHK_UNDETERMINED)

    # region 控制全选check的状态
    # 添加部分的全选
    def on_all_add_check_change(self, event):
        add_check = self.add_check
        add_list = self.add_file_list
        self.all_check_change(add_check, add_list)

    # 删除部分的全选
    def on_all_del_check_change(self, event):
        del_check = self.del_check
        del_list = self.del_file_list
        self.all_check_change(del_check, del_list)

    # 变更部分的全选
    def on_all_chg_check_change(self, event):
        chg_check = self.chg_check
        chg_list = self.chg_file_list
        self.all_check_change(chg_check, chg_list)

    # 冲突部分的全选
    def on_all_cfl_check_change(self, event):
        cfl_check = self.cfl_check
        cfl_list = self.cfl_file_list
        self.all_check_change(cfl_check, cfl_list)

    # 管理外的部分的全选
    def on_all_unv_check_change(self, event):
        unv_check = self.unv_check
        unv_list = self.unv_file_list
        self.all_check_change(unv_check, unv_list)

    # 公用逻辑函数
    def all_check_change(self, check, list):
        for i in range(0, list.GetCount()):
            list.Check(i, check.IsChecked())
    # endregion

    def generate_layout(self, list_sizer, file_list_ctrl, check):
        file_list_sizer = wx.BoxSizer(wx.VERTICAL)
        file_list_sizer.Add(check, 0, wx.SHAPED)
        file_list_sizer.Add(file_list_ctrl, 1, wx.EXPAND)
        # 把所有的文件列表sizer放入统一的sizer管理
        list_sizer.Add(file_list_sizer, 1, wx.EXPAND)

    def set_items(self, file_list_dic):
        add_file_list = file_list_dic.get('add_list')
        del_file_list = file_list_dic.get('del_list')
        chg_file_list = file_list_dic.get('chg_list')
        cfl_file_list = file_list_dic.get('cfl_list')
        unv_file_list = file_list_dic.get('unv_list')
        add_list = self.add_file_list
        del_list = self.del_file_list
        chg_list = self.chg_file_list
        cfl_list = self.cfl_file_list
        unv_list = self.unv_file_list
        add_list.AppendItems(add_file_list)
        del_list.AppendItems(del_file_list)
        chg_list.AppendItems(chg_file_list)
        cfl_list.AppendItems(cfl_file_list)
        unv_list.AppendItems(unv_file_list)


class VCSPanel(wx.ScrolledWindow):
    def __init__(self, parent, id=wx.ID_ANY):
        wx.ScrolledWindow.__init__(self, parent=parent, id=id, pos=(0, 0), style=wx.HSCROLL | wx.VSCROLL)
        v_sizer = wx.BoxSizer(wx.VERTICAL)
        self.notebook = notebook = wx.Notebook(self, style=wx.NB_TOP)
        self.file_list = file_list = FileListPanel(notebook, id=wx.ID_ANY)
        self.notebook.AddPage(file_list, 'Check In List', select=True)
        self.SetSizer(v_sizer)
        v_sizer.Add(notebook, 1, wx.EXPAND)
        self.SetVirtualSize((800, 500))
        self.SetScrollRate(20, 20)

    def set_items(self, file_list):
        self.file_list.set_items(file_list)


# test code for debug
if __name__ == '__main__':

    app = wx.App()
    frame = wx.Frame(None)
    panel = VCSPanel(frame, id=wx.ID_ANY)
    dic = {
        'add_list': ['01', '02'],
        'del_list': ['11', '12'],
        'chg_list': ['21', '22'],
        'cfl_list': ['31', '32'],
        'unv_list': ['41', '42'],
    }
    panel.set_items(dic)

    app.SetTopWindow(frame)
    frame.Show()
    app.MainLoop()
